package ui.window;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import util.JDialogUtil;

@SuppressWarnings("serial")
public class JDialogEnter extends JDialog{

	private JPasswordField passWord;
	
	private JButton btnOk;
	
	private String pwd = "aiyaoyao";
	
	public JDialogEnter(){
		//设置模态
		this.setModal(true);
		//设置标题
		this.setTitle("输入密码");
		//设置大小
		this.setSize(256,64);
		//居中
		JDialogUtil.setDialogCenter(this);
		//设置不可改变大小
		this.setResizable(false);
		//设置布局
		this.setLayout(new BorderLayout());
		//初始化组件
		this.createCom();
	}

	/**
	 * 显示密码框
	 */
	public void showJDialogEnter(){
		this.setVisible(true);
	}
	
	/**
	 * 初始化组件
	 */
	private void createCom() {
//北部面板
		
		
//南部面板	
		//创建南部面部（流式布局）
		JPanel south = new JPanel(new FlowLayout());
		//创建输入框
		this.passWord = new JPasswordField(10);
		passWord.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == 10) {
					if(isPass()) {
						setVisible(false);
					}
				}
			}
		});
		//添加输入框到南部面板
		south.add(passWord);
		//创建确定按钮
		this.btnOk = new JButton("确定");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		//按钮添加到南部面板
		south.add(btnOk);
		//南部面板添加到主面板
		this.add(south, BorderLayout.SOUTH);
	}

	/**
	 * 密码验证
	 */
	public boolean isPass() {
		String passWords = new String(this.passWord.getPassword());
		if(pwd.equals(passWords)) {
			return true;
		}
		return false;
	}
}
